package com.artPreter.service.impl;


import com.artPreter.mapper.ContentEventRelativeMapper;
import com.artPreter.mapper.EventExhibitionMapper;
import com.artPreter.pojo.*;
import com.artPreter.pojo.EventExhibition;
import com.artPreter.service.IEventExhibitionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 活动展览表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2025-01-05
 */
@Service
public class EventExhibitionServiceImpl extends ServiceImpl<EventExhibitionMapper, EventExhibition> implements IEventExhibitionService {

    @Autowired
    private ContentEventRelativeMapper contentEventRelativeMapper;

    @Override
    public List<EventExhibition> getList(EventExhibition eventExhibition) {
        LambdaQueryWrapper<EventExhibition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EventExhibition::getStatus,1);
        List<EventExhibition> result= this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public EventExhibition getInfo(EventExhibition eventExhibition) {
        LambdaQueryWrapper<EventExhibition> queryWrapper = new LambdaQueryWrapper<>();
        if(eventExhibition.getEventId()!=null&&!"".equals(eventExhibition.getEventId())){
            queryWrapper.eq(EventExhibition::getEventId,eventExhibition.getEventId());
        }else if(eventExhibition.getId()!=null){
            queryWrapper.eq(EventExhibition::getId,eventExhibition.getId());
        }else {
            return null;
        }
        List<EventExhibition> list = this.baseMapper.selectList(queryWrapper);
        if(list.size()==1){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public IPage<EventExhibition> getPage(Integer currentPage, Integer size, EventExhibition eventExhibition) {
        IPage<EventExhibition> eventExhibitionIPage = new Page<>(currentPage, size);
        LambdaQueryWrapper<EventExhibition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EventExhibition::getStatus,1);
        eventExhibitionIPage = this.baseMapper.selectPage(eventExhibitionIPage, queryWrapper);
        return eventExhibitionIPage;
    }

    @Override
    public void add(EventExhibition eventExhibition) {
        eventExhibition.setStatus(1);
        // 生成UUID并设置到实体对象中
        String uuid = UUID.randomUUID().toString();
        eventExhibition.setEventId(uuid);
        this.baseMapper.insert(eventExhibition);
    }

    @Override
    public void edit(EventExhibition eventExhibition) {
        this.baseMapper.updateById(eventExhibition);
    }

    @Override
    public void delete(EventExhibition eventExhibition) {
        eventExhibition.setStatus(0);
        this.baseMapper.updateById(eventExhibition);
    }

    @Override
    public IPage<EventExhibition> getPageNoRelative(Integer currentPage, Integer size, EventExhibition eventExhibition) {
        LambdaQueryWrapper<EventExhibition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EventExhibition::getStatus,1);
        queryWrapper.orderByDesc(EventExhibition::getId);
        queryWrapper.eq(EventExhibition::getDepartId,eventExhibition.getDepartId());
        // 内容id
        String contentId = eventExhibition.getContentId();
        queryWrapper.notExists("SELECT 1 FROM jv_content_event_relative WHERE jv_content_event_relative.content_id = '"+contentId+"' and jv_content_event_relative.event_id = jv_event_exhibition.event_id");
        IPage<EventExhibition> eventExhibitionIPage = new Page<>(currentPage,size);

        // 图片名字模糊搜索
        if(eventExhibition.getName()!=null&&!"".equals(eventExhibition.getName())){
            queryWrapper.like(EventExhibition::getName,eventExhibition.getName());
        }

        eventExhibitionIPage = this.baseMapper.selectPage(eventExhibitionIPage, queryWrapper);
        return eventExhibitionIPage;
    }

    @Override
    public IPage<EventExhibition> getPageRelative(Integer currentPage, Integer size, EventExhibition eventExhibition) {
        System.out.println("获取关联");
        LambdaQueryWrapper<EventExhibition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EventExhibition::getStatus,1);
        queryWrapper.orderByDesc(EventExhibition::getId);
        queryWrapper.eq(EventExhibition::getDepartId,eventExhibition.getDepartId());
        // 内容id
        String contentId = eventExhibition.getContentId();
        queryWrapper.exists("SELECT 1 FROM jv_content_event_relative WHERE jv_content_event_relative.content_id = '"+contentId+"' and jv_content_event_relative.event_id = jv_event_exhibition.event_id");
        IPage<EventExhibition> eventExhibitionIPage = new Page<>(currentPage,size);

        // 图片名字模糊搜索
        if(eventExhibition.getName()!=null&&!"".equals(eventExhibition.getName())){
            queryWrapper.like(EventExhibition::getName,eventExhibition.getName());
        }
        eventExhibitionIPage = this.baseMapper.selectPage(eventExhibitionIPage, queryWrapper);
        return eventExhibitionIPage;
    }
    @Override
    public void addToRelative(EventExhibition eventExhibition) {
        System.out.println("添加关联");
        ContentEventRelative contentEventRelative = new ContentEventRelative();
        if(eventExhibition.getEventId()!=null&&!"".equals(eventExhibition.getEventId())){
            contentEventRelative.setEventId(eventExhibition.getEventId());
        }else {
            return;
        }
        if(eventExhibition.getContentId()!=null&&!"".equals(eventExhibition.getContentId())){
            contentEventRelative.setContentId(eventExhibition.getContentId());
        }else {
            return;
        }
        contentEventRelativeMapper.insert(contentEventRelative);
    }

    @Override
    public void removeEventExhibitionRelative(EventExhibition eventExhibition) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(eventExhibition.getEventId()!=null&&!"".equals(eventExhibition.getEventId())){
            queryWrapper.eq("event_id",eventExhibition.getEventId());
        }else {
            return;
        }
        if(eventExhibition.getContentId()!=null&&!"".equals(eventExhibition.getContentId())){
            queryWrapper.eq("content_id", eventExhibition.getContentId());
        }else {
            return;
        }
        contentEventRelativeMapper.delete(queryWrapper);
    }
}
